#!/bin/bash

__FileName__=""
__Author__="SuoSuo"
__Time__="2023/08/08"
__Version__="V2.0"


#显示程序的版本号和更新日期
version () {
	printf "\033[34m
=============================================================
*   ${PROGRAM_NAME} ${PROGRAM_VERSION}
*   ${AUTHOR_NAME}          ${AUTHOR_NUMBER}
=============================================================
Release Note:
    >>V0.2 20210616
    >>V0.4 20220214
    >>V0.7 20220703
    >>V1.0 20220924
    >>V1.3 20230726
    >>V2.0 20230808

---------------------------------------------------------------
    \033[0m\n"
	exit 0
}

format_centre_print () {
:<<!
para 1: 要打印的字符串
para 2: 占位数
para 3:
para 4: 是否换行，默认否 0/1
!
    Ss=$1
    Le=$2
    Nn=$(( (Le-${#Ss}) / 2 ))
    [ $Nn -lt 0 ] && Nn=0
    pre=$Nn
    suf=$Nn
    fff=$3
    [[ -n "$3" && "$3" -ge 1 ]] && suf=$((suf - fff))
    format_str="%${pre}s%s%${suf}s"
    [[ "$4" -eq 1 ]] && format_str=${format_str}"\n"
    printf "\033[34m${format_str}\033[0m"  "" "${Ss}" ""
#STR="eeeesfsfssfseee"; printf 'ddd %11s%-11s dddd \n' `echo $STR | cut -c 1-$((${#STR}/2))` `echo $STR | cut -c $((${#STR}/2+1))-${#STR}`
}
title_print () {
    while read line
    do
        name=`echo $line | awk '{print $1}' | xargs`
        len=`echo $line | cut -d' ' -f2 | xargs`
        suff=`echo $line | cut -d' ' -f3 | xargs`
        format_centre_print $name $len $suff
    done <<< $1
}

net_card_bus  () {
    lspci | egrep -i 'ether' -q
    [[ $? -ne 0 ]] && { printf "\033[31mThe Machine Does Not Have a Network Adapter!!\033[0m\n" ; return; }
    printf "\033[32m                          NetWork Card Adapter     \033[0m\n"
#    printf '[root@SuoSuo ~]#\033[35m lspci -v | grep -i  ether\033[0m\n'
    lspci -v 2> /dev/null | grep -i ether
    read -r -d '' titles <<- EOF
Name     10
Status   14
Bus    8
DriName  18
DriVer   10
FwVer  32
EOF
    title_print "${titles}"
    if [ ${SHOW_VERBOSE} -ge 1 ];then
        read -r -d '' titles <<- EOF
Slot 6
Numa  4
VD   8
SubVD 12
EOF
        title_print "${titles}"
    fi
    printf "\n"
#    printf "\033[34m%-12s %-7s %-14s %-13s %-16s %-22s%5s%5s%6s%10s\033[0m\n"  "  Name" " Status"  "     Bus"  "DriName"  "DriVer" "         FwVer"  "Slot" "Numa" "VD" "SubVD"
    for i in `cat /proc/net/dev | egrep -iv '^[ ]*lo|virbr|vnet' |sed '3,$p' -n | cut -d: -f1 | sort`
    do
        is_mellanox=0
        is_lspci=1
        printf "%-16s" $i
        state=`ip addr  show $i | sed '1p' -n  | awk -F state '{print $2}' | awk '{print $1}'`
        printf "%-5s" $state
        bus=`ethtool -i $i | grep -i bus | awk -F info: '{print $2}'`
        printf "%-13s" $bus
        lspci -s ${bus} &> /dev/null
        if [[ $? -ne 0 ]]; then
            is_lspci=0
        else
            # 判断是否是 Mellanox 卡
            lspci -s $bus | grep -i 'mellanox' -q
            [[ $? -eq 0 ]] && is_mellanox=1
        fi
        driver_name=`ethtool -i $i | sed '1p' -n | awk -F river: '{print $2}'`
        printf "%-14s" $driver_name
        driver_ver=`ethtool -i $i | sed '2p' -n | awk -F sion: '{print $2}'`
        printf "%-17s" $driver_ver
        fw_version=`ethtool -i $i | sed '3p' -n | cut -d: -f2`
        if [ $is_mellanox -eq 1 ]; then
            fw_version=`echo $fw_version | awk '{print $1}'`
        fi
        printf "%-28s" "$fw_version"
        if [ $is_lspci -ne 1 ]; then
            printf "\n"
            continue
        fi
        if [ ${SHOW_VERBOSE} -ge 1 ];then
            ids_str=`lspci -nvmms ${bus} 2> /dev/null`
            #slots=`lspci -v -s ${bus} 2> /dev/null | grep 'Physical Slot' | awk '{print $NF}'`
            slots=`echo ${ids_str} | awk -F 'PhySlot' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-3s" "${slots}"
            numanode=`echo ${ids_str} | awk -F 'NUMANode' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-3s" "${numanode}"
            vendor=`echo ${ids_str} | awk -F 'Vendor' '{print $2}' | awk '{print $2}' | xargs`
            device=`echo ${ids_str} | awk -F 'Device' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-10s" "${vendor}:${device}"
            svendor=`echo ${ids_str} | awk -F 'SVendor' '{print $2}' | awk '{print $2}' | xargs`
            sdevice=`echo ${ids_str} | awk -F 'SDevice' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-10s" "${svendor}:${sdevice}"
        fi
        printf "\n"
        if [ ${SHOW_VERBOSE} -ge 2 ];then
            lspci_str=`lspci -vv -s ${bus} 2> /dev/null  | xargs -i echo "{}===" | xargs`
            printf "\033[33m%17s\033[0m" "LnkCap:"
            pci_LnkCap=`echo ${lspci_str} | awk -F 'LnkCap:' '{print $2}' | awk -F'===' '{print $1}' | awk -F',' '{ii=1;while(ii<=NF){if($ii ~ "Speed|Width")printf("%-16s",$ii);ii++}print " ";}' | xargs`
            printf "%-40s\n" " ${pci_LnkCap}"
            printf "\033[33m%17s\033[0m" "LnkSta:"
            pci_LnkSta=`echo ${lspci_str} | awk -F 'LnkSta:' '{print $2}' | awk -F'===' '{print $1}' | awk -F',' '{ii=1;while(ii<=NF){if($ii ~ "Speed|Width")printf("%-16s",$ii);ii++}print " ";}' | xargs`
            printf "%-40s\n" " ${pci_LnkSta}"
        fi
    done 2> /dev/null
}


#当前时间，格式是“年.月.日.时.分.秒”
date_time=`date +%Y.%m.%d.%H.%M.%S`
#文件名称
PROGRAM_NAME=$(basename $0)
#作者名字
AUTHOR_NAME="SuoSuo"
#作者工号
AUTHOR_NUMBER="50023"
# 版本
PROGRAM_VERSION="V2.0"
# 首地址
BASE_DIR=`pwd`
SHOW_VERBOSE=0

#显示帮助目录菜单
usage_and_exit () {
    echo
    printf "${PROGRAM_NAME} Version ${PROGRAM_VERSION}\n"
	printf "\\033[32m
    Network Adapter Information Profile
Usage:
  $PROGRAM_NAME [options]
Options:
    -v           Be Verbose
    -h           Show Help Message
    -b           Show Version
    \n\
Attention:
    1>.
    2>.
Examples:
    ./${PROGRAM_NAME} -v
    ./${PROGRAM_NAME} -vvv
Questions:
    1>.
    2>.   \
    \033[0m\n
"
    exit 1
}

while getopts "vhb" arg
do
	case $arg in
	    v)
	    let SHOW_VERBOSE++ ;;
		h)
		usage_and_exit ;;
		b)
		version ;;
	esac
done

main () {
    net_card_bus
}

main